/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson6;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
 *
 * @author netcracker
 */
public class JArrayListVector implements IVector, Cloneable, Serializable {

    private ArrayList<Double> arlst;

    public JArrayListVector() {
        arlst = new ArrayList<>();
    }

    public JArrayListVector(double... d) {
        arlst = new ArrayList<>();
        for (int i = 0; i < d.length; i++) {
            arlst.add(d[i]);
        }
    }

    @Override
    public void add(double o) {
        this.arlst.add(o);
    }

    @Override
    public void delete(int index) {
        this.arlst.remove(index);
    }

    @Override
    public double get(int id) {

        return this.arlst.get(id);
    }

    @Override
    public void set(double value, int position) {
        this.arlst.set(position, value);
    }

    @Override
    public void fill(double[] mass) {
        for (int i = 0; i < mass.length; i++) {
            this.arlst.add(mass[i]);
        }
    }

    @Override
    public void fill(IVector myVector) {
        for (int i = 0; i < myVector.getLength(); i++) {
            arlst.add(myVector.get(i));
        }
    }

    @Override
    public boolean isSame(IVector myVector) {
        if (this.getLength() == myVector.getLength()) {

            for (int i = 0; i < this.getLength(); i++) {

                boolean b = this.get(i) == myVector.get(i);
                if (b == false) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int getLength() {

        return arlst.size();
    }

    @Override
    public void multiply(double f) {
        for (int i = 0; i < this.arlst.size(); i++) {
            arlst.set(i, arlst.get(i) * f);
        }
    }

    @Override
    public void plus(IVector llv) {
        for (int i = 0; i < this.arlst.size(); i++) {
            arlst.set(i, arlst.get(i) + llv.get(i));
        }
    }

    @Override
    public void sort() {
        Double[] tmp = (Double[]) this.arlst.toArray();
        Arrays.sort(tmp);
        for (int i = 0; i < tmp.length; i++) {
            arlst.set(i, tmp[i]);
        }
    }

    @Override
    public void swap(int i, int j) {
        Double t = arlst.get(i);
        arlst.set(i, arlst.get(j));
        arlst.set(j, t);
    }

    @Override
    public Iterator<Double> iterator() {
        return new Iterator<Double>() {
            int marker = 0;

            @Override
            public boolean hasNext() {
                if (arlst.size() > marker) {
                    return true;
                }
                return false;
            }

            @Override
            public Double next() {
                double result = arlst.get(marker);
                marker++;

                return result;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };
    }

    @Override
    public boolean equals(Object obj) {
        obj = (IVector) obj;
        if (obj == null) {
            return false;
        }

        if (!(obj instanceof IVector)) {
            return false;
        }
        final IVector other = (IVector) obj;

        if (this.getLength() != other.getLength()) {
            return false;
        }
        if (this.hashCode() != obj.hashCode()) {
            return false;
        }

        if (!this.isSame(other)) {
            return false;
        }

        return true;
    }

    @Override
    public JArrayListVector clone() throws CloneNotSupportedException {
        JArrayListVector result;
        result = (JArrayListVector) super.clone();
        result.arlst = (ArrayList<Double>) arlst.clone();
        return result;
    }

    @Override
    public int hashCode() {
        long result = Double.doubleToLongBits(this.arlst.get(0));

        int ups;
        int downs;
        for (int i = 1; i < this.getLength() - 1; i++) {
            result = result ^ Double.doubleToLongBits(this.get(i));

        }
        downs = (int) result;
        result = result >> 32;
        ups = (int) result;
        return downs ^ ups;
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer(this.getClass().toString());
        stringBuffer.append("\n" + "Contains:" + "\n");
        for (int i = 0; i < this.arlst.size(); i++) {
            stringBuffer.append(this.arlst.get(i)).append("\n");
        }
        return stringBuffer.toString();
    }

}
